<?php

namespace Admin\Controller;

class OrderController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $order_no = I('post.order_no');
        $orderStatus = I('post.orderStatus');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');

        $store_id = session("wadmin");

        if ($store_id['store_id'] == 0) {
            $where = '';
        } else {
            $where['store_id'] = $store_id['store_id'];
        }

        if ($order_no) {
            $where['order_no'] = array('like', "%$order_no%");
        }

        if ($orderStatus) {
            if ($orderStatus != -1) {
                $where['order_state'] = $orderStatus;
            }
        }

//        if(empty($order_no)&&empty($orderStatus)){
//            $where="1=1";
//        }

        if ($start_time && $end_time) {
            $where['time'] = array(array('egt', strtotime($start_time)), array('elt', strtotime($end_time)), 'AND');
        }

        $where['valid_flag'] = 1;

        $count = M("dishes_order")
            ->where($where)
            ->order('time desc')
            ->count();

        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出

        $result = M("dishes_order")
            ->field("*")
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('time desc')
            ->select();

        foreach ($result as $key => $v) {
            switch ($v['order_state']) {
                case 1:
                    $result[$key]['fromart_state'] = '待付款';
                    break;
                case 2:
                    $result[$key]['fromart_state'] = '已付款';
                    break;
                default:
                    $result[$key]['fromart_state'] = '待付款';
            }
            $result[$key]['fromart_method'] = '微信';
            $result[$key]['pay_time'] = date('Y-m-d', $v['time']);

            $store_name = M('store')->field('store_name')->where(['store_id' => $v['store_id']])->find();
            $result[$key]['store_name'] = $store_name['store_name'];
        }

        $result_result = M("dishes_order")
            ->field("sum(order_price) as order_price,count(id) as count")
            ->where($where)
            ->find();
        //查询订单的状态
        $this->assign("end_time", $end_time);
        $this->assign("start_time", $start_time);
        $this->assign("result_result", $result_result);
        $this->assign("result", $result);
        $this->assign('order_no', $order_no);
        $this->assign('orderStatus', $orderStatus);
        $this->assign("page", $show); // 赋值分页输出
        $this->display();
    }

    /**
     * 订单删除
     */
    public function del()
    {
        $order_no = I("get.id");
        $data['valid_flag'] = 2;
        $result = M('dishes_order')->where(["order_no" => $order_no])->save($data);
        if ($result > 0) {
            $this->success("操作成功");
        } else {
            $this->error("操作失败");
        }
    }

    /**
     * 更改状态为已支付
     */
    public function payComplete()
    {
        $id = I('get.id');
        $result = M('order')->where("id='$id'")->setField('state', 1);
        if ($result) {
            $this->success("操作成功");
        } else {
            $this->error("操作失败");
        }
    }

    /**
     * 订单详情
     */
    public function edit()
    {
        $order_no = I('get.id');
        //查询所有订单
        $order = M('dishes_order')->where("order_no = '$order_no'")->find();
        if ($order) {
            $state = $order['order_state'];
            if ($state == 1) {
                $order['fromart_state'] = '待付款';
            }
            if ($state == 2) {
                $order['fromart_state'] = '已付款';
            }
        }

        $orderDetail = M('order_detail')->alias('o')
            ->join("join meal_dishes as d on d.id = o.dish_id")
            ->field('o.order_no,o.dish_id,o.price,o.count,o.amount,o.practice,d.id as dishId,d.image,d.category_id,d.name')
            ->where(["order_no" => $order_no])
            ->order("o.create_time desc")
            ->select();

        $this->assign('order_no', $order_no);
        $this->assign('order', $order);
        $this->assign('orderDetail', $orderDetail);
        $this->display();
    }

    /**
     * 订单运营概览
     */
    public function statistics()
    {
        $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));

        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $where = '';
            $storeId = 0;
        } else {
            $where['store_id'] = $store_id['store_id'];
            $storeId = $store_id['store_id'];
        }

        $storess = I("post.store_id");
        if ($storess) {
            $where['store_id'] = array("eq", $storess);
        }
        $start = I("post.start_time");
        if ($start) {
            $where['time'] = array("egt", strtotime($start));
        }
        $end = I("post.end_time");
        if ($end) {
            $where['time'] = array("elt", strtotime($end));
        }
        if (!($start && $end)) {
            $where['time'] = array("between", array($beginThismonth, $endThismonth));
        }
        $result = M("dishes_order")->field("sum(total_price) as total_price2,FROM_UNIXTIME(time,'%Y-%m-%d') as time2")->where($where)->group("time")->select();
        foreach ($result as $key => $val) {
            $date[] = $val['time2'];
            $total_enroll[] = $val['total_price2'];
        }
        $date2 = "'" . join("','", $date) . "'";
        $total_enroll2 = "'" . join("','", $total_enroll) . "'";

        $start = M("store")->select();
        $this->assign('start', $start);
        $this->assign('date', $date2);
        $this->assign('storeId', $storeId);
        $this->assign('total_enroll', $total_enroll2);
        $this->display();
    }

    /**
     * 每个月的天数
     * @return array
     */
    public function dateDay()
    {
        $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $begin = date("d", $beginThismonth);
        $end = date("d", $endThismonth);
        for ($i = $begin; $i <= $end; $i++) {
            $date[] = date("Y-m-" . $i, $endThismonth);
        }
        return $date;
    }
}